/**
 * 2015年9月7日上午10:09:07
 * GarbageDataProcess.javaGarbageDataProcess
 */
package com.service.calculate.vehicle;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.service.bean.device.Vehicle;
import com.service.bean.garbage.GarbageBean;
import com.service.bean.garbage.SendWeight;
import com.service.cache.CacheManager;
import com.service.garbage.ningbo.GarbageAddress;
import com.service.other.Logger;
import com.service.socket.protocol.Packet;

/**
 * 垃圾称重协议 解析、计算线程
 * 2015年9月7日上午10:09:07
 *
 * 张
 */
public class GarbageDataProcess implements Runnable{

	@Override
	public void run() {
		try {
			ConcurrentLinkedQueue<Packet> packets = CacheManager.instance().getGarbageDataSaveQueue(Thread.currentThread().getName());
			while ((packets.peek()) != null) {
				Packet packet = packets.poll();
				Vehicle vehicle= CacheManager.instance().getVehicle(packet.getDeviceNo());
				// 计算垃圾称重数据
				GarbageCal cal = new GarbageCal(packet, vehicle);
				List<GarbageBean> list = cal.cal();
				double count = 0;
				for(GarbageBean bean :list){
					count +=bean.getCollectionAmt();
					//DbGarbage.savaGarbage(bean,vehicle.company); 
					CacheManager.instance().getGarbageQueue(vehicle.company).offer(bean);
					if("032".equals(vehicle.company.code)){
						//向宁波第三方发送数据
						String service ="CarWeightService";
						String str = "SendWeight";
						String sendMessage = new SendWeight(vehicle,bean).toString();
						GarbageAddress ga = new GarbageAddress(vehicle,service,str, sendMessage);
						ga.sendWeight();		
					}
				}
				//如果两桶垃圾重量都为空，则报警
				if(list.size()!=0 && count==0){
					CacheManager.instance().getGarbageAlarmQueue(vehicle.company).offer(list.get(0));
				}
			}
			Thread.currentThread().interrupt();
		} catch (Exception e) {
			Logger.err(e);
		}
	}

}
